package com.huopaolan.yohuoshop.Mvp.View.Fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.design.widget.TabLayout.OnTabSelectedListener;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.huopaolan.yohuoshop.Mvp.View.Activity.HomeActivity;
import com.huopaolan.yohuoshop.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClassifyFragment extends Fragment implements OnTabSelectedListener, OnClickListener {
    private View inflate;
    private Fragment current;
    private CategoryFragment categoryFragment;
    private BrandFragment brandFragment;
    private TabLayout tabLayout;
    private ImageView imageView;
    private RadioButton men, wowen, kids, blk;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragment_classify, container, false);
        initView(inflate);
        return inflate;
    }

    private void initView(View inflate) {
        tabLayout = inflate.findViewById(R.id.classify_tab);
        tabLayout.addTab(tabLayout.newTab().setText("品类"));
        tabLayout.addTab(tabLayout.newTab().setText("品牌"));
        men = inflate.findViewById(R.id.men);
        wowen = inflate.findViewById(R.id.wowen);
        kids = inflate.findViewById(R.id.kids);
        blk = inflate.findViewById(R.id.blk);
        imageView = inflate.findViewById(R.id.iv_home_fragment_sliding_menu_left);
        imageView.setOnClickListener(this);
        men.setOnClickListener(this);
        wowen.setOnClickListener(this);
        kids.setOnClickListener(this);
        blk.setOnClickListener(this);
        tabLayout.setOnTabSelectedListener(this);
        categoryFragment = new CategoryFragment();
        brandFragment = new BrandFragment();
        FragmentSelect(categoryFragment);
    }

    private void FragmentSelect(Fragment fragment) {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        if (current != fragment) {
            if (current != null && current.isAdded()) {
                transaction.hide(current);
            }
            if (!fragment.isAdded()) {
                transaction.add(R.id.classify_fragment, fragment, "");
                transaction.show(fragment);
                transaction.commit();
            } else {
                transaction.show(fragment);
                transaction.commit();
            }
            current = fragment;
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        boolean brand = tab.getText().equals("品类");
        if (brand) {
            FragmentSelect(categoryFragment);
        } else {
            FragmentSelect(brandFragment);
        }
    }

    @Override
    public void onStart() {
        Window window = getActivity().getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.men:
                men.setChecked(true);
//                FragmentSelect(brandFragment);
                break;
            case R.id.wowen:
//                FragmentSelect(brandFragment);
                break;
            case R.id.kids:
//                FragmentSelect(brandFragment);
                break;
            case R.id.blk:
//                FragmentSelect(brandFragment);
                break;
            case R.id.iv_home_fragment_sliding_menu_left:
                HomeActivity.slidingMenu.showMenu();
                break;
        }
    }

    public static void SetMenu(int position) {

    }
}
